package com.crazy.service.sys.impl;

import static com.alibaba.fastjson.JSON.toJSONString;
import static org.hibernate.criterion.MatchMode.ANYWHERE;
import static org.hibernate.criterion.Restrictions.eq;
import static org.hibernate.criterion.Restrictions.ilike;
import static org.hibernate.criterion.Restrictions.isNull;
import static org.springframework.util.StringUtils.hasText;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.inject.Inject;
import javax.transaction.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import com.crazy.common.constants.Const;
import com.crazy.common.ztree.TreeNode;
import com.crazy.dao.sys.PubResourceDao;
import com.crazy.entity.sys.PubResource;
import com.crazy.service.sys.PubResourceService;

/**
 * 资源Service实现类
 * 
 * @author Zhang Weiwei
 * @since 2013-9-20下午1:49:55
 */
@Service
@Transactional
public class PubResourceServiceImpl extends BaseServiceImpl<PubResource>
		implements PubResourceService {
	@Inject
	private PubResourceDao dao;

	@Override
	public String listToJsonStringForAdd(Collection<PubResource> list) {
		Collection<TreeNode> nodes = new ArrayList<TreeNode>();
		TreeNode node = null;
		for (PubResource res : list) {
			node = new TreeNode();
			node.setId(res.getResourceId());
			node.setName(res.getResourceName());
			if (res.getPubResource() != null) {
				node.setpId(res.getPubResource().getResourceId());
			}
			node.setOpen(true);
			nodes.add(node);
		}
		return toJSONString(nodes);
	}

	@Override
	public String listToJsonStringForUpdate(Collection<PubResource> list,
			PubResource pubResource) {
		Collection<TreeNode> nodes = new ArrayList<TreeNode>();
		TreeNode node = null;
		for (PubResource res : list) {
			node = new TreeNode();
			node.setId(res.getResourceId());
			node.setName(res.getResourceName());
			if (res.getPubResource() != null) {
				node.setpId(res.getPubResource().getResourceId());
			}
			if (pubResource.getPubResource() != null) {
				if (res.getResourceId().equals(
						pubResource.getPubResource().getResourceId())) {
					node.setChecked(true);
				}
			}
			if (res.getResourceId().equals(pubResource.getResourceId())) {
				node.setChkDisabled(true);
			}
			node.setOpen(true);
			nodes.add(node);
		}
		return toJSONString(nodes);
	}

	@Override
	public String findMenuListToJsonString(String userId, String contextPath) {
		List<PubResource> list = this.dao.findByUserId(userId,
				Const.ENABLE.getStatus(), Const.MENU.getType());
		Collection<TreeNode> nodes = new ArrayList<TreeNode>();
		TreeNode node = null;
		for (PubResource res : list) {
			if (res.getPubResource() != null) {
				node = new TreeNode();
				node.setId(res.getResourceId());
				node.setName(res.getResourceName());
				node.setpId(res.getPubResource().getResourceId());
				if (res.getResourceUrl() != null) {
					node.setUrl(contextPath.concat(res.getResourceUrl()));
				}
				node.setTarget(res.getTarget());
				nodes.add(node);
			}
		}
		return toJSONString(nodes);
	}

	@Override
	public PubResource findRootPubResource() {
		DetachedCriteria dc = dao.getDetachedCriteria();
		dc.add(isNull("pubResource"));
		return this.dao.findUniqueResult(dc);
	}

	@Override
	public Page<PubResource> findPubResource(Integer pageNo, Integer pageSize,
			PubResource resource, Order... orders) {
		DetachedCriteria dc = dao.getDetachedCriteria();
		if (hasText(resource.getResourceType())) {
			dc.add(eq("resourceType", resource.getResourceType()));
		}
		if (hasText(resource.getResourceName())) {
			dc.add(ilike("resourceName", resource.getResourceName(), ANYWHERE));
		}
		if (hasText(resource.getResourceUrl())) {
			dc.add(ilike("resourceUrl", resource.getResourceUrl(), ANYWHERE));
		}
		if (hasText(resource.getAuthorityName())) {
			dc.add(ilike("authorityName", resource.getAuthorityName(), ANYWHERE));
		}
		return dao.findByDetachedCriteria(pageNo, pageSize, dc, orders);
	}
}
